#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

CURRENT_BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
# 受保护的分支
PROTECTED_BRANCH_NAME="master"

# 禁止合并的分支
FORBIDDEN_BRANCH_NAMES=("test" "pre")

# 校验是否能合并
function validCanMerge() {
   if [[ "$GIT_REFLOG_ACTION" == *"$1"* ]]; then
      echo "检测到非法合并: ${GIT_REFLOG_ACTION//merge / } ==into==> $CURRENT_BRANCH_NAME"
      echo "撤销合并中..."
      $(git reset --merge HEAD@{1})
      echo "已撤销合并 done"
      exit 1
   fi
}

 if [[ ! "${FORBIDDEN_BRANCH_NAMES[@]}"  =~ "$CURRENT_BRANCH_NAME" ]]; then
      for branchName in $FORBIDDEN_BRANCH_NAMES  
      do
         validCanMerge $branchName 
      done
 fi

